<template>
  <div class="profile-page">
    <div class="content-wrapper">
      <el-card class="profile-card">
        <template #header>
          <div class="card-header">
            <h2>个人信息</h2>
            <el-button type="primary" @click="handleEdit">编辑资料</el-button>
          </div>
        </template>

        <div class="info-section">
          <div class="avatar-section">
            <el-avatar :size="100" :src="userInfo.avatar">
              {{ userInfo.username?.charAt(0)?.toUpperCase() }}
            </el-avatar>
          </div>

          <div class="info-list">
            <div class="info-item">
              <span class="label">用户名：</span>
              <span class="value">{{ userInfo.username }}</span>
            </div>
            <div class="info-item">
              <span class="label">邮箱：</span>
              <span class="value">{{ userInfo.email }}</span>
            </div>
            <div class="info-item">
              <span class="label">手机号：</span>
              <span class="value">{{ userInfo.phone || '未设置' }}</span>
            </div>
            <div class="info-item">
              <span class="label">注册时间：</span>
              <span class="value">{{ formatDate(userInfo.createTime) }}</span>
            </div>
          </div>
        </div>
      </el-card>



      <!-- 编辑个人信息对话框 -->
      <el-dialog
        v-model="dialogVisible"
        title="编辑个人信息"
        width="500px"
      >
        <el-form
          ref="formRef"
          :model="form"
          :rules="rules"
          label-width="80px"
        >
          <el-form-item label="用户名" prop="username">
            <el-input v-model="form.username" />
          </el-form-item>
          <el-form-item label="邮箱" prop="email">
            <el-input v-model="form.email" />
          </el-form-item>
          <el-form-item label="手机号" prop="phone">
            <el-input v-model="form.phone" />
          </el-form-item>
        </el-form>
        <template #footer>
          <span class="dialog-footer">
            <el-button @click="dialogVisible = false">取消</el-button>
            <el-button type="primary" @click="handleSubmit">确定</el-button>
          </span>
        </template>
      </el-dialog>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useUserStore } from '@/stores/user'
import { ElMessage } from 'element-plus'
import request from '@/utils/request'

const userStore = useUserStore()
const userInfo = ref({})
const statistics = ref({
  totalSubmissions: 0,
  lastWeekSubmissions: 0,
  todaySubmissions: 0
})

const dialogVisible = ref(false)
const formRef = ref(null)
const form = ref({
  username: '',
  email: '',
  phone: ''
})

const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 2, max: 20, message: '长度在 2 到 20 个字符', trigger: 'blur' }
  ],
  email: [
    { required: true, message: '请输入邮箱地址', trigger: 'blur' },
    { type: 'email', message: '请输入正确的邮箱地址', trigger: 'blur' }
  ],
  phone: [
    { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号', trigger: 'blur' }
  ]
}

const formatDate = (dateStr) => {
  if (!dateStr) return ''
  return new Date(dateStr).toLocaleString()
}

const loadUserInfo = async () => {
  try {
    const res = await request.get('/api/user/info')
    if (res.code === 200) {
      userInfo.value = res.data
    }
  } catch (error) {
    console.error('加载用户信息失败:', error)
  }
}

const loadStatistics = async () => {
  try {
    const res = await request.get('/api/user/statistics')
    if (res.code === 200) {
      statistics.value = res.data
    }
  } catch (error) {
    console.error('加载统计信息失败:', error)
  }
}

const handleEdit = () => {
  form.value = {
    username: userInfo.value.username,
    email: userInfo.value.email,
    phone: userInfo.value.phone
  }
  dialogVisible.value = true
}

const handleSubmit = async () => {
  if (!formRef.value) return

  try {
    await formRef.value.validate()
    const res = await request.put('/api/user/profile', form.value)
    if (res.code === 200) {
      ElMessage.success('更新成功')
      dialogVisible.value = false
      loadUserInfo()
    }
  } catch (error) {
    console.error('更新用户信息失败:', error)
    if (error.response?.status === 401) {
      ElMessage.warning('请先登录')
    } else {
      ElMessage.error('更新失败')
    }
  }
}

onMounted(() => {
  loadUserInfo()
  loadStatistics()
})
</script>

<style lang="scss" scoped>
.profile-page {
  .content-wrapper {
    max-width: 1000px;
    margin: 0 auto;
    padding: 20px;
  }

  .profile-card {
    margin-bottom: 20px;

    .card-header {
      display: flex;
      justify-content: space-between;
      align-items: center;

      h2 {
        margin: 0;
        font-size: 20px;
        color: #303133;
      }
    }

    .info-section {
      display: flex;
      gap: 40px;
      padding: 20px 0;

      .avatar-section {
        display: flex;
        flex-direction: column;
        align-items: center;
        gap: 10px;
      }

      .info-list {
        flex: 1;

        .info-item {
          margin-bottom: 15px;
          font-size: 16px;

          .label {
            color: #909399;
            margin-right: 10px;
          }

          .value {
            color: #303133;
          }
        }
      }
    }
  }

  .statistics-section {
    margin-top: 20px;

    .stat-card {
      text-align: center;
      padding: 20px;

      .stat-value {
        font-size: 24px;
        font-weight: bold;
        color: #409EFF;
        margin-bottom: 10px;
      }

      .stat-label {
        color: #909399;
        font-size: 14px;
      }
    }
  }
}

// 响应式设计
@media screen and (max-width: 768px) {
  .profile-page {
    .info-section {
      flex-direction: column;
      align-items: center;
      gap: 20px;

      .info-list {
        width: 100%;
      }
    }

    .statistics-section {
      .el-col {
        margin-bottom: 10px;
      }
    }
  }
}
</style>
